package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class JDBCUtil
{
  private static String url = "jdbc:mysql://localhost:3306/shoukuan?characterEncoding=utf-8";
  private static String name = "root";
  private static String password = "root";
  private static Connection connnection = null;
  private PreparedStatement preparedStatement = null;
  private ResultSet resultSet = null;
  private static JDBCUtil jdbcUtil = null;

  static
  {
    try
    {
      Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  public static JDBCUtil getInitJDBCUtil()
  {
    if (jdbcUtil == null) {
      synchronized (JDBCUtil.class) {
        if (jdbcUtil == null) {
          jdbcUtil = new JDBCUtil();
        }
      }
    }
    return jdbcUtil;
  }

  public Connection getConnection()
  {
    try
    {
      connnection = DriverManager.getConnection(url, name, password);
    } catch (SQLException e) {
      System.out.println("数据库连接失败！");
    }
    return connnection;
  }

  public int executeUpdate(String sql, Object[] params)
  {
    int affectedLine = 0;
    try
    {
      connnection = getConnection();

      this.preparedStatement = connnection.prepareStatement(sql);

      if (params != null) {
        for (int i = 0; i < params.length; i++) {
          this.preparedStatement.setObject(i + 1, params[i]);
        }
      }

      affectedLine = this.preparedStatement.executeUpdate();
    }
    catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    finally {
      closeAll();
    }
    return affectedLine;
  }

  private ResultSet executeQueryRS(String sql, Object[] params)
  {
    try
    {
      connnection = getConnection();

      this.preparedStatement = connnection.prepareStatement(sql);

      if (params != null) {
        for (int i = 0; i < params.length; i++) {
          this.preparedStatement.setObject(i + 1, params[i]);
        }
      }

      this.resultSet = this.preparedStatement.executeQuery();
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return this.resultSet;
  }

  public List<Object> excuteQuery(String sql, Object[] params)
  {
    ResultSet rs = executeQueryRS(sql, params);
    ResultSetMetaData rsmd = null;
    int columnCount = 0;
    try {
      rsmd = rs.getMetaData();
      columnCount = rsmd.getColumnCount();
    } catch (SQLException e1) {
      System.out.println(e1.getMessage());
    }

    List list = new ArrayList();
    try
    {
      while (rs.next()) {
        Map map = new HashMap();
        for (int i = 1; i <= columnCount; i++) {
          map.put(rsmd.getColumnLabel(i), rs.getObject(i));
        }
        list.add(map);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    finally {
      closeAll();
    }
    return list;
  }

  public Object executeQuerySingle(String sql, Object[] params)
  {
    Map map = new HashMap();
    try
    {
      connnection = getConnection();

      this.preparedStatement = connnection.prepareStatement(sql);

      if (params != null) {
        for (int i = 0; i < params.length; i++) {
          this.preparedStatement.setObject(i + 1, params[i]);
        }
      }

      this.resultSet = this.preparedStatement.executeQuery();
      ResultSetMetaData rsmd = null;

      int columnCount = 0;
      try {
        rsmd = this.resultSet.getMetaData();

        columnCount = rsmd.getColumnCount();
      } catch (SQLException e1) {
        System.out.println(e1.getMessage());
      }
      if (this.resultSet.next()) {
        for (int i = 1; i <= columnCount; i++)
          map.put(rsmd.getColumnLabel(i), this.resultSet.getObject(i));
      }
    }
    catch (SQLException e)
    {
      System.out.println(e.getMessage());
    } finally {
      closeAll();
    }
    return map;
  }

  private void closeAll()
  {
    if (this.resultSet != null) {
      try {
        this.resultSet.close();
      } catch (SQLException e) {
        System.out.println(e.getMessage());
      }

    }

    if (this.preparedStatement != null) {
      try {
        this.preparedStatement.close();
      } catch (SQLException e) {
        System.out.println(e.getMessage());
      }

    }

    if (connnection != null)
      try {
        connnection.close();
      } catch (SQLException e) {
        System.out.println(e.getMessage());
      }
  }
}